﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.IO;

namespace ASBCC.Workflow.K2Manager
{
    public class SerializableDictionary : Dictionary<string, object>, IXmlSerializable
    {
        #region IXmlSerializable 成员

        public XmlSchema GetSchema()
        {
            return null;
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="reader"></param>
        public void ReadXml(XmlReader reader)
        {
            if (reader.IsEmptyElement || !reader.Read())
            {
                return;
            }
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                reader.ReadStartElement("item");
                string key = reader.ReadElementString("key");

                reader.ReadStartElement("value");
                string strValue = reader.Value;
                reader.MoveToAttribute("type");
                string valueType = reader.Value;

                object value;
                if (valueType == "null")
                {
                    value = null;
                }
                else
                {
                    value = Convert.ChangeType(strValue, Type.GetType(valueType));
                }
                reader.ReadEndElement();
                reader.MoveToContent();
                this.Add(key, value);
            }
            reader.ReadEndElement();
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="writer"></param>
        public void WriteXml(XmlWriter writer)
        {
            foreach (string key in this.Keys)
            {
                object value = this[key];
                writer.WriteStartElement("item");
                writer.WriteElementString("key",  key);

                writer.WriteStartElement("value");
                if (value == null)
                {
                    writer.WriteAttributeString("type", "null");
                }
                else
                {
                    writer.WriteAttributeString("type", value.GetType().Name);
                    writer.WriteValue(value);
                }
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
        }

        #endregion
    }

    public class SerializableDictionaryHelper
    {
        public static string DictionarySerializer(object obj, System.Type type)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer ser = new XmlSerializer(type);
                ser.Serialize(sw, obj);
                return sw.ToString();
            }
        }

        public static object DictionaryDeserialize(Type type, string str)
        {
            using (StringReader sr = new StringReader(str))
            {
                XmlSerializer xz = new XmlSerializer(type);
                return xz.Deserialize(sr);
            }
        }
    }

}
